#!/bin/bash

nginxDir="/etc/nginx/sites-available/";
cd $nginxDir;

if [ ! -n "$1" ];then
	echo "缺少参数";exit;
fi

if [ $1 == '-h' ];then
    echo "#########################################################"
    echo "参数功能："
    echo "./nginx-manage.sh ls 列出所有nginx配置文件"
    echo "./nginx-manage.sh checkout laravel.conf 将default文件替换成laravel.conf"
    echo "./nginx-manage.sh laravel.conf 如果不存在，则复制default,并vim打开，保存为laravel.conf"
    echo "./nginx-manage.sh rm laravel.conf 删除laravel.conf配置文件"
    echo "#########################################################"
    exit;
fi

if [ "$1" == "ls" ]
then
  ls -al
elif [ "$1" == "checkout" ]
then
    if [ -n "$2" ];then # 判断是否存在该参数
        if [ ! -f "$2" ];then 
            sudo cp default "$2" && sudo vim "$2"
        else
            sudo cp "$2" default && sudo nginx -s reload
        fi
    else
        echo "checkout 缺少配置文件参数！"
    fi
elif [ "$1" == "rm" ]
then
    if [ -n "$2" ];then # 判断是否存在该参数
        if [ ! -f "$2" ];then 
            echo "没有该配置文件:$2"
        else
            sudo rm "$2"
			echo "删除 $2 配置成功！"
			exit
        fi
    else
        echo "rm 缺少配置文件参数！"
    fi
fi